﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
using AvaloniaApplication1.Helper;

namespace AvaloniaApplication1.Configs
{
    public class ProjectConfig
    {

        #region 单例模式

        public static readonly string ConfigFileName = "PF-PV.xml";

        private static readonly object m_LockSetInstance = new object();

        private static ProjectConfig m_Instance;

        public static ProjectConfig Instance
        {
            get { return m_Instance; }
        }

        public ProjectConfig()
        {

        }

        static ProjectConfig()
        {
            try
            {
                XDocument xdoc = XDocument.Load(ConfigFileName);
                lock (m_LockSetInstance)
                {
                    m_Instance = XmlHelp.DeXMLSerialize<ProjectConfig>(xdoc.ToString());
                }
            }
            catch (Exception exception)
            {

            }
        }


        public static void ReGetValue()
        {

            XDocument xdoc = XDocument.Load(ConfigFileName);
            lock (m_LockSetInstance)
            {
                m_Instance = XmlHelp.DeXMLSerialize<ProjectConfig>(xdoc.ToString());
            }
        }

       

        public int LastSelectIndex = 0;
        #endregion

        /// <summary>
        /// 保存配置文件
        /// </summary>
        public void Save()
        {
            string projectConfig = XmlHelp.ToString(this, this.GetType(), Encoding.Default);
            XDocument xDocument = XDocument.Parse(projectConfig);
            xDocument.Save($"{AppDomain.CurrentDomain.BaseDirectory}{ConfigFileName}");
        }


    }
}
